package project1;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class BallPanel extends JPanel{
	private static final long serialVersionUID = 4130447187829973980L;
	private static final int BALL_SIZE = 20;
	private int stepX;
	private int stepY;
	
	
	private Graphics graphics;
	
	PlayerPad playerPad = new PlayerPad ();
	ComputerPad computerPad = new ComputerPad ();
	Ball ball = new Ball();
	
	
	public void drawPlayer() {
		int y = playerPad.getPlayerPadPosition();
		graphics.fillRect( 0, y, playerPad.getPadWidth(), playerPad.getPlayerPadSize());
	}
	
	public void drawComputer() {
		int y = computerPad.getPlayerPadPosition();
		graphics.fillRect( getWidth() - computerPad.getPadWidth(), y, computerPad.getPadWidth(), computerPad.getPlayerPadSize());
		
	}
	
	public void setBallAtTheMiddleOfTheGameField(){
		
		ball.setBallPositionX(getWidth()/2);
		ball.setBallPositionY(getHeight()/2);
	}
	
	public void drawBall (){
		graphics.fillOval(ball.getBallPositionX(), ball.getBallPositionY(), 20, 20);
	}
	
	public void SetInitialPositions(){
		drawPlayer();
		drawComputer();
		setBallAtTheMiddleOfTheGameField();
		drawBall();
	}
	
	public void movePlayerDown(){
		if (playerPad.getPlayerPadPosition() < getHeight() - playerPad.getPlayerPadSize())
        {
			int position = playerPad.getPlayerPadPosition()+10;
            playerPad.setPlayerPadPosition(position);
        }
	}
	public void movePlayerUp(){
		
		if (playerPad.getPlayerPadPosition() > 0)
        {
			int position = playerPad.getPlayerPadPosition() - 10;
            playerPad.setPlayerPadPosition(position);
        }
	}
	public void moveComputerUpAndDown(){
		int position = ball.getBallPositionY() - computerPad.getPlayerPadSize()/2;
		computerPad.setPlayerPadPosition(position);
	}
	
	
	
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		graphics = g;
		setBackground(Color.BLUE);
		graphics.setColor(Color.RED);
		drawBall();
		drawPlayer();
		drawComputer();
		
	}
	
	public void moveBall() {
		if (ball.getBallPositionY() < 0 )
        {
           ball.setBallDirectionUp(false);
        }
		if (ball.getBallPositionY() > getHeight() - 20)
        {
			ball.setBallDirectionUp(true);
        }
		if (ball.getBallPositionX() > getWidth() - 20 )
        {
			setBallAtTheMiddleOfTheGameField();
            ball.setBallDirectionRight(false);
            ball.setBallDirectionUp(true);
            //firstPlayerResult++;
            graphics.setColor(Color.GREEN);
            graphics.drawString("First player wins!", getWidth()/2, getHeight()/2);
            
            //Console.ReadKey();
        }
		if (ball.getBallPositionX() < 0)
        {
            setBallAtTheMiddleOfTheGameField();
            ball.setBallDirectionRight(true);
            ball.setBallDirectionUp(true);
            //secondPlayerResult++;
            graphics.setColor(Color.GREEN);
            graphics.drawString("Computer wins!", 200, 200);
           
        }
		
		if (ball.getBallPositionX() < 10 )
        {
            if (ball.getBallPositionY() >= playerPad.getPlayerPadPosition()
                && ball.getBallPositionY() < playerPad.getPlayerPadPosition() + playerPad.getPlayerPadSize())
            {
                ball.setBallDirectionRight(true);
            }
        }
		
		if (ball.getBallPositionX() >= getWidth() -30 )
        {
            if (ball.getBallPositionY()>= computerPad.getPlayerPadPosition()
                && ball.getBallPositionY() < computerPad.getPlayerPadPosition() + computerPad.getPlayerPadSize())
            {
                ball.setBallDirectionRight(false);
            }
        }
		
		
		if (ball.getBallDirectionUp())
        {
			int temp = ball.getBallPositionY()- 10;
            ball.setBallPositionY(temp);
        }
        else
        {
        	int temp = ball.getBallPositionY()+ 10;
        	ball.setBallPositionY(temp);
        }
        if (ball.getBallDirectionRight())
        {
        	int temp = ball.getBallPositionX() + 10;
        	ball.setBallPositionX(temp);
        }
        else
        {
        	int temp = ball.getBallPositionX() - 10;
        	ball.setBallPositionX(temp);
        }
	}
	
	

	
	public void setStepX(int stepX) {
		this.stepX = stepX;
	}
	
	public void setStepY(int stepY) {
		this.stepY = stepY;
	}

	


}
